<?php


class createFile extends CI_Controller
{
    private $compName;
    private $tableName;
    public function __construct()
    {
        parent::__construct();

        $this->compName = "容百科技";
        $this->tableName = "weixininfo_rb";
    }
    //创建目录
    /*
       *  @param  createfile //创建文件夹
       *  @param  createpath  // 创建的路径
       *  @param  file_exists() // 查看是否文件夹有同样的目录
       *  @param  file // 创建的的路径 基于文件夹 ./Public/Uploads/  下创建修改
       *  @param  mkdir // 创建文件夹的函数
       *  @param 2021/10/12  8:57
       */
    function createfile($file) {
        $_createpath = './html/images/upload/' . $file;
        $returnpath = 'html/images/upload/' . $file;
//        $_createpath = iconv('utf-8', 'gb2312', $createpath);
        if (file_exists($_createpath) == false) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            if (mkdir($_createpath, 0700, true)) {
                $value['file'] ='文件夹创建成功';
                $value['success']='success';
            } else {
                $value['file'] ='文件夹创建失败';
                $value['fail']='fail';
            }
        } else {
            $value['file'] ='文件夹已存在';
            $value['fail']='fail';
        }
        return $returnpath;
    }

    function downloadfile(){
        $mediaid = $_REQUEST['mediaid'];
        //$this->Data_model->update_data(array('type' => 'jsapi_ticket'),array('jsapi_ticket' => $mediaid),'weixininfo_rb');
        $access_token = $this->get_access_token();
        $url = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=".$access_token."&media_id=".$mediaid;
        $fileInfo = $this->downloadWeixinFile($url);
        $savePath = $this->createfile(date("Ymd"), time());
        $filename = $savePath. "/" .time()."_".$mediaid.".jpg";
        $this->saveWeixinFile($filename, $fileInfo["body"]);
        echo $filename;
    }
    function downloadWeixinFile($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);    //只取body头
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        $imageAll = array_merge(array('header' => $httpinfo), array('body' => $package));
        return $imageAll;
    }

    function saveWeixinFile($filename, $filecontent)
    {
        $local_file = fopen($filename, 'w');
        if (false !== $local_file){
            if (false !== fwrite($local_file, $filecontent)) {
                fclose($local_file);
            }
        }
    }

    function get_access_token(){
        $result = $this->Data_model->get_single_data(array("compname"=>$this->compName,"type"=>"access_token"),$this->tableName);
        if($result["time"] < time() || $result === null){
            $corpid = $result["corpid"];
            $corpsecret = $result["corpsecret"];
            if($result === null){
                $corpid = $this->appId;
                $corpsecret = $this->appSecret;
            }
            $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;

            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data["access_token"])){
                if($result === null){
                    $this->Data_model->add_data(array("compname"=>$this->compName,"corpid" => $this->appId,"corpsecret" => $this->appSecret,"type"=>"access_token","access_token" => $data["access_token"]),$this->tableName);
                }else{
                    $this->Data_model->update_data(array("compname"=>$this->compName,"type"=>"access_token"),array("access_token" => $data["access_token"],"time" => time()+7000),$this->tableName);
                }
                $access_token = $data["access_token"];
            }else{
                return "获取access_token错误";
            }
        }else{
            $access_token = $result["access_token"];
        }
        return $access_token;
    }

    function http_request_json($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}